﻿using System;
using System.Collections.Generic;

namespace Quail.Html
{
    public class WebUnorderedList : WebControl
    {
        /// <summary>
        /// Initializes a new instance of the WebUnorderedList class.
        /// </summary>
        /// <param name="container">The owning html container</param>
        /// <param name="selector">The jQuery selector for the control</param>
        public WebUnorderedList(WebControlContainer container, string selector)
            : base(container, selector)
        {
        }

        /// <summary>
        /// Initializes a new instance of the WebUnorderedList class.
        /// </summary>
        /// <param name="control">The control for which the WebUnorderedList should take over</param>
        public WebUnorderedList(WebControl control)
            : base(control)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebUnorderedList"/> class.
        /// </summary>
        public WebUnorderedList()
        {
        }

        /// <summary>
        /// Gets the list of LI items within the UL tag
        /// </summary>
        public List<string> ListItems
        {
            get
            {
                var task = ActiveTask.BeginTask("ListItems");
                try
                {
                    var children = GetControls<WebControl>("li");
                    var result = new List<string>();

                    foreach (var child in children)
                    {
                        result.Add(child.InnerText);
                    }

                    task.Finish();

                    return result;
                }
                catch (Exception e)
                {
                    task.Finish(e);
                    throw;
                }
            }
        }
    }
}
